package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.*;
import cn.com.hhrcw.mapstruct.*;
import cn.com.hhrcw.params.ApplicationHistoryParams;
import cn.com.hhrcw.params.EntryParam;
import cn.com.hhrcw.params.OpDeliveryHistoryParams;
import cn.com.hhrcw.position.entity.RecruitmentPosition;
import cn.com.hhrcw.position.service.IRecruitmentPositionService;
import cn.com.hhrcw.service.*;
import cn.com.hhrcw.utils.DateUtil;
import cn.com.hhrcw.vo.ApplicationHistoryVO;
import cn.com.hhrcw.vo.CvResumeVO;
import cn.com.hhrcw.vo.SsUserInfoVO;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.com.hhrcw.utils.DateUtil.getLastMonths;

/**
 * 简历库
 *
 * @program: recruit-boot-parent
 *
 * @author: JD
 * @create: 2020-11-06 15:05
 **/
@RestController
@RequestMapping("/admin/entryInfo")
@Slf4j
@Validated
public class AdminEntryController {

    @Autowired
    private IOpJobService jobService;

    @Autowired
    private IOpDeliveryHistoryService deliveryHistoryService;

    @Autowired
    private AdminOpJobConvert jobConvert;

    @Autowired
    AdminService enterpriseService;

    @Autowired
    private IPiInfoService piInfoService;

    @Autowired
    private ICvUserEduInfoService eduInfoService;

    @Autowired
    private AdminCvUserEduInfoConvert eduInfoConvert;

    @Autowired
    private AdminPiInfoConvert enPiInfoConvert;

    @Autowired
    private AdminOpEntryHistoryConvert entryHistoryConvert;

    @Autowired
    private ICvUserWorkInfoService workInfoService;

    @Autowired
    private AdminCvUserWorkInfoConvert workInfoConvert;

    @Autowired
    private ICvWorkIntentionService workIntentionService;

    @Autowired
    private AdminCvWorkIntentionConvert workIntentionConvert;

    @Autowired
    private AdminOpDeliveryHistoryConvert deliveryHistoryConvert;

    @Autowired
    private AdminOpInterviewHistoryConvert interviewHistoryConvert;

    @Autowired
    private IEnInfoService enInfoService;

    @Autowired
    private IOpInterviewHistoryService interviewHistoryService;

    @Autowired
    private ICvCertificateService certificateService;

    @Autowired
    private AdminCvCertificateConvert cvCertificateConvert;

    @Autowired
    private ICvResumeService resumeService;

    @Autowired
    private AdminCvResumeConvert resumeConvert;

    @Autowired
    private IOpBrowseHistoryService browseHistoryService;

    @Autowired
    private EnInfoAdminConvert enInfoAdminConvert;

    @Autowired
    private IOpInterviewContactService contactService;

    @Autowired
    private IEnContactsService enContactsService;

    @Autowired
    AdminEnContactsConvert contactsConvert;

    @Autowired
    IOpEntryHistoryService entryHistoryService;

    @Autowired
    ISysUserService userService;

    @Autowired
    IRecruitmentPositionService positionService;

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 批量个人用户
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/delete")
    @AutoLog(value = "简历库删除", logType = 2, ty = "delete")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result delete(String id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<PiInfo> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(PiInfo::getUserId, id.split(","))
                .set(PiInfo::getDelFlag, true)
                .set(PiInfo::getUpdateBy, sysUser.getId())
                .set(PiInfo::getUpdateTime, cn.hutool.core.date.DateUtil.date());

        LambdaUpdateWrapper<SysUser> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.in(SysUser::getId, id.split(","))
                .set(SysUser::getDelFlag, true)
                .set(SysUser::getUpdateBy, sysUser.getId())
                .set(SysUser::getUpdateTime, cn.hutool.core.date.DateUtil.date());

        piInfoService.update(updateWrapper);
        userService.update(userLambdaUpdateWrapper);
        //投递记录
        LambdaUpdateWrapper<OpDeliveryHistory> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(OpDeliveryHistory::getUserId, id.split(",")).set(OpDeliveryHistory::getDelFlag, false);
        deliveryHistoryService.update(lambdaUpdateWrapper);

        //面试记录
        LambdaUpdateWrapper<OpInterviewHistory> opInterviewHistoryLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        opInterviewHistoryLambdaUpdateWrapper.in(OpInterviewHistory::getUserId, id.split(",")).set(OpInterviewHistory::getDelFlag, false);
        interviewHistoryService.update(opInterviewHistoryLambdaUpdateWrapper);

        //入职记录
        LambdaUpdateWrapper<OpEntryHistory> entryHistoryLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        entryHistoryLambdaUpdateWrapper.in(OpEntryHistory::getUserId, id.split(",")).set(OpEntryHistory::getDelFlag, false);
        entryHistoryService.update(entryHistoryLambdaUpdateWrapper);
        return Result.ok();
    }

    /**
     * 人才库-人才库
     *
     * @param params
     * @param request
     * @return
     */
    @GetMapping("/getUserListInfo")
    @AutoLog(logType = 2, ty = "visit", value = "人才库列表")
    public Result<IPage<SsUserInfoVO>> getUserListInfo(EntryParam params, HttpServletRequest request) {
        Map<String, Object> queryMap = new HashMap<>(16);
        if(StrUtil.isNotBlank(params.getJobName())){
            List<RecruitmentPosition> positions = positionService.lambdaQuery()
                .select(RecruitmentPosition::getId)
                .like(RecruitmentPosition::getName, params.getJobName())
                .list();
            queryMap.put("positions", positions.stream().map(RecruitmentPosition::getId).toArray());
        }
        if (StrUtil.isNotBlank(params.getDataId())) {
            queryMap.put("deliveryId", params.getDataId().split(","));
        }
        queryMap.put("sysFrom", params.getSysFrom());
        queryMap.put("sex", params.getSex());
        queryMap.put("bsType", params.getBsType());
        queryMap.put("bsId", params.getSsId());
        queryMap.put("maritalStatus", params.getMaritalStatus());
        queryMap.put("state", null);
        int five = 5;
        if (params.getState() != null) {
            if (params.getState() == 0) {
                queryMap.put("state", new Integer[]{0, 1});
            } else if (params.getState() == five) {
                queryMap.put("state", new Integer[]{5, 9});
            } else {
                queryMap.put("state", new Integer[]{params.getState()});
            }
        }
        queryMap.put("name", params.getUserName());
        //            应聘时间范围 0 近三个月 1近半年
        queryMap.put("createTimeBegin", null);
        queryMap.put("createTimeEnd", null);
        Date now = new Date();
        if (params.getTimeState() != null && params.getTimeState() == 0) {
            queryMap.put("createTimeBegin", cn.hutool.core.date.DateUtil.offsetMonth(now, -3));
        } else if (params.getTimeState() != null && params.getTimeState() == 1) {
            queryMap.put("createTimeBegin", cn.hutool.core.date.DateUtil.offsetMonth(now, -6));
        }
        queryMap.put("createTimeEnd", now);
        //工作经验
        if (params.getMinWorkYear() != null && params.getMaxWorkYear() != null) {
            if (params.getMinWorkYear() == 0 && params.getMaxWorkYear() == 0) {
                queryMap.put("jobTimeBegin", null);
                queryMap.put("jobTimeEnd", null);
            } else {
                DateTime start = cn.hutool.core.date.DateUtil.offsetMonth(now, -(params.getMaxWorkYear() * 12));
                DateTime end = cn.hutool.core.date.DateUtil.offsetMonth(now, -(params.getMinWorkYear() * 12));
                queryMap.put("jobTimeBegin", start.toString(DatePattern.NORM_DATE_PATTERN));
                queryMap.put("jobTimeEnd", end.toString(DatePattern.NORM_DATE_PATTERN));
            }
        }
        //期望月薪
        if (params.getMinMoney() != null && params.getMaxMoney() != null) {
            queryMap.put("minMoney", params.getMinMoney());
            queryMap.put("maxMoney", params.getMaxMoney());
        }
        if (StrUtil.isNotBlank(params.getWorkIndustry())) {
            queryMap.put("workIndustry", params.getWorkIndustry() + ",");
        }
        //年龄
        if (params.getMinAge() != null) {
            String endBirthday = DateUtil.getYearFirst(params.getMinAge() - 1);
            queryMap.put("endBirthday", endBirthday);

        }
        if (params.getMaxAge() != null) {
            String beginBirthday = DateUtil.getYearFirst(params.getMaxAge());
            queryMap.put("beginBirthday", beginBirthday);
        }

        if (StrUtil.isNotBlank(params.getRecruitType())) {
            queryMap.put("recruitType", params.getRecruitType());
        }

        IPage<OpDeliveryHistory> opDeliveryHistoryPage = this.deliveryHistoryService.resumeTalentList(params.page(), queryMap);
        IPage<SsUserInfoVO> convert = opDeliveryHistoryPage.convert(opDeliveryHistory -> {
            SsUserInfoVO vo = new SsUserInfoVO();
            vo.setDataId(opDeliveryHistory.getId());
            //12.12修改为工作经历  院校信息  求职意向信息为中文简历种的信息
            CvResume resumeEn = resumeService.getById(opDeliveryHistory.getResumeId());
            LambdaQueryWrapper<CvUserEduInfo> eduQuery = new LambdaQueryWrapper<>();
            eduQuery.eq(CvUserEduInfo::getDelFlag, false).eq(CvUserEduInfo::getResumeId, resumeEn.getId()).orderByDesc(CvUserEduInfo::getEduLevel);
            List<CvUserEduInfo> eduInfos = eduInfoService.list(eduQuery);
            vo.setEduInfo(eduInfoConvert.toVO(eduInfos));
            OpJob job = jobService.getById(opDeliveryHistory.getJobId());
            vo.setJobName(job.getJobName());
            vo.setJobTime(opDeliveryHistory.getCreateTime());
            CvResume resume = resumeService.getById(opDeliveryHistory.getResumeId());
            List<CvResumeVO> resumevos = new ArrayList<>();
            resumevos.add(resumeConvert.toVO(resume));
            vo.setCvResumeList(resumevos);
            vo.setReId(resume.getId());
            LambdaQueryWrapper<PiInfo> infoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            infoLambdaQueryWrapper.eq(PiInfo::getDelFlag, false).eq(PiInfo::getUserId, opDeliveryHistory.getUserId());
            PiInfo piInfo = piInfoService.getOne(infoLambdaQueryWrapper);
            vo.setUserInfo(enPiInfoConvert.toVO(piInfo));
            //期望行业
            LambdaQueryWrapper<CvUserWorkInfo> workInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            workInfoLambdaQueryWrapper.eq(CvUserWorkInfo::getDelFlag, false)
                .eq(CvUserWorkInfo::getResumeId, opDeliveryHistory.getResumeId())
                .orderByDesc(CvUserWorkInfo::getCreateTime);
            List<CvUserWorkInfo> workInfos = workInfoService.list(workInfoLambdaQueryWrapper);
            vo.setWorkInfo(workInfoConvert.toVO(workInfos));
            vo.getUserInfo().setWorkYears(piInfo.getWorkYears());
            LambdaQueryWrapper<CvWorkIntention> workIntentionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            workIntentionLambdaQueryWrapper.eq(CvWorkIntention::getDelFlag, false)
                .eq(CvWorkIntention::getResumeId, opDeliveryHistory.getResumeId())
                .orderByDesc(CvWorkIntention::getCreateTime);
            List<CvWorkIntention> workIntentions = workIntentionService.list(workIntentionLambdaQueryWrapper);
            vo.setWorkIntention(workIntentionConvert.toVO(workIntentions));
            //证书
            List<CvCertificate> certificateList = certificateService.lambdaQuery()
                .eq(CvCertificate::getDelFlag, false)
                .eq(CvCertificate::getUserId, piInfo.getUserId())
                .list();
            vo.setCertificateList(this.cvCertificateConvert.toVO(certificateList));
            //浏览量
            int viewCount = browseHistoryService.lambdaQuery().eq(OpBrowseHistory::getDelFlag, false).eq(OpBrowseHistory::getUserId, opDeliveryHistory.getUserId()).count();
            vo.setBrowseCount(viewCount);
            int deCount = deliveryHistoryService.lambdaQuery().eq(OpDeliveryHistory::getDelFlag, false).eq(OpDeliveryHistory::getUserId, opDeliveryHistory.getUserId()).count();
            if (viewCount != 0) {
                vo.setDeliveryRate(new BigDecimal(deCount).divide(new BigDecimal(viewCount), 2).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
            return vo;
        });
        return Result.ok(convert);
    }

    /**
     * 专场-人才库
     *
     * @param params
     * @param request
     * @return
     */
    @GetMapping("/getUserList")
    @AutoLog(logType = 2, ty = "visit", value = "专场人才库列表")
    public Result<Page<SsUserInfoVO>> getUserList(EntryParam params, HttpServletRequest request) {
        LambdaQueryWrapper<OpJob> jobLambdaQueryWrapper = new LambdaQueryWrapper<>();
        jobLambdaQueryWrapper.eq(OpJob::getDelFlag, false);

        if (StringUtils.isNotBlank(params.getSsId())) {
            jobLambdaQueryWrapper.eq(OpJob::getBsType, 1).eq(OpJob::getBsId, params.getSsId());
        }
        if (StringUtils.isNotBlank(params.getJobId())) {
            jobLambdaQueryWrapper.eq(OpJob::getId, params.getJobId());
        }
        if (StringUtils.isNotBlank(params.getJobName())) {
            jobLambdaQueryWrapper.like(OpJob::getJobName, params.getJobName());
        }
        if (StringUtils.isNotBlank(params.getComName())) {
            List<EnInfo> enInfos = enInfoService.lambdaQuery().like(EnInfo::getEnName, params.getComName()).eq(EnInfo::getDelFlag, false).list();
            if (CollectionUtils.isEmpty(enInfos)) {
                return Result.ok(new Page<>());
            } else {
                List<String> comIds = new ArrayList<>();
                enInfos.forEach(e -> {
                    comIds.add(e.getId());
                });
                jobLambdaQueryWrapper.in(OpJob::getCompanyId, comIds);
            }

        }

        if (params.getBsType() != null) {
            jobLambdaQueryWrapper.eq(OpJob::getBsType, params.getBsType());
        }
        if (StrUtil.isNotBlank(params.getRecruitType())) {
            jobLambdaQueryWrapper.eq(OpJob::getRecruitType, params.getRecruitType());
        }
        if (params.getSysFrom() != null) {
            jobLambdaQueryWrapper.eq(OpJob::getSysFrom, params.getSysFrom());
        }
        List<OpJob> jobs = jobService.list(jobLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(jobs)) {
            List<String> jobIds = new ArrayList<>();
            jobs.forEach(e -> {
                jobIds.add(e.getId());
            });
            LambdaQueryWrapper<OpDeliveryHistory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OpDeliveryHistory::getDelFlag, false).in(OpDeliveryHistory::getJobId, jobIds);

            int five = 5;
            if (StringUtils.isNotBlank(params.getConName()) && params.getState() == five) {
                List<EnContacts> contacts = enContactsService.lambdaQuery().like(EnContacts::getUserName, params.getConName()).eq(EnContacts::getDelFlag, false).list();
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(contacts)) {
                    return Result.ok(new Page<>());
                }
                List<String> conIds = new ArrayList<>();
                contacts.forEach(enContacts -> {
                    conIds.add(enContacts.getId());
                });
                List<OpEntryHistory> histories = entryHistoryService.lambdaQuery().in(OpEntryHistory::getContactId, conIds).eq(OpEntryHistory::getDelFlag, false).select(OpEntryHistory::getJobId, OpEntryHistory::getUserId).list();
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(histories)) {
                    return Result.ok(new Page<>());
                }
                queryWrapper.and(query -> {
                    histories.forEach(e -> {
                        query.or(queryIn -> queryIn.eq(OpDeliveryHistory::getJobId, e.getJobId()).eq(OpDeliveryHistory::getUserId, e.getUserId()));
                    });
                    return query;
                });
            }
            int four = 4;
            if (StringUtils.isNotBlank(params.getConName()) && params.getState() == four) {
                List<EnContacts> contacts = enContactsService.lambdaQuery().like(EnContacts::getUserName, params.getConName()).eq(EnContacts::getDelFlag, false).list();
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(contacts)) {
                    return Result.ok(new Page<>());
                }
                List<String> conIds = new ArrayList<>();
                contacts.forEach(enContacts -> {
                    conIds.add(enContacts.getId());
                });
                List<OpInterviewHistory> histories = interviewHistoryService.lambdaQuery().in(OpInterviewHistory::getContactId, conIds).eq(OpInterviewHistory::getDelFlag, false).select(OpInterviewHistory::getJobId, OpInterviewHistory::getUserId).list();
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(histories)) {
                    return Result.ok(new Page<>());
                }
                queryWrapper.and(query -> {
                    histories.forEach(e -> {
                        query.or(queryIn -> queryIn.eq(OpDeliveryHistory::getJobId, e.getJobId()).eq(OpDeliveryHistory::getUserId, e.getUserId()));
                    });
                    return query;
                });
            }

            /**
             * 0 未阅读 1 已阅读  2 不合适 3 通过筛选 4  面试 5已发offer(已录用)6 已入职 7 被过滤
             */
            int five1 =5;
            if (params.getState() != null) {
                if (params.getState() == 0) {
                    queryWrapper.in(OpDeliveryHistory::getState, new Integer[]{0, 1});
                } else if (params.getState() == five1) {
                    queryWrapper.in(OpDeliveryHistory::getState, new Integer[]{5, 9});
                } else {
                    queryWrapper.eq(OpDeliveryHistory::getState, params.getState());
                }
            }

            if (StringUtils.isNotBlank(params.getUserName())) {
                LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
                infoQuery.like(PiInfo::getName, params.getUserName()).eq(PiInfo::getDelFlag, false);
                List<PiInfo> piInfoList = piInfoService.list(infoQuery);
                if (CollectionUtils.isEmpty(piInfoList)) {
                    return Result.ok(new Page<>());
                } else {
                    List<String> userIds = new ArrayList<>();
                    piInfoList.forEach(e -> {
                        userIds.add(e.getUserId());
                    });
                    queryWrapper.in(OpDeliveryHistory::getUserId, userIds);
                }
            }

            if (params.getEduLevel() != null) {
//0 初中  1 高中 2 中专/中技 3 大专 4 本科 5 硕士 6 MBA/EMBA  7 博士
                LambdaQueryWrapper<CvUserEduInfo> eduQuery = new LambdaQueryWrapper<>();
                eduQuery.eq(CvUserEduInfo::getDelFlag, false).eq(CvUserEduInfo::getEduLevel, params.getEduLevel());
                List<CvUserEduInfo> eduInfos = eduInfoService.list(eduQuery);
                if (CollectionUtils.isEmpty(eduInfos)) {
                    return Result.ok(new Page<>());
                } else {
                    List<String> userIds = new ArrayList<>();
                    eduInfos.forEach(e -> {
                        userIds.add(e.getUserId());
                    });
                    queryWrapper.in(OpDeliveryHistory::getUserId, userIds);
                }
            }

            if (params.getMaritalStatus() != null) {
                LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
                infoQuery.ge(PiInfo::getMaritalStatus, params.getMaritalStatus());
                List<PiInfo> piInfoList = piInfoService.list(infoQuery);
                if (CollectionUtils.isEmpty(piInfoList)) {
                    return Result.ok(new Page<>());
                } else {
                    List<String> userIds = new ArrayList<>();
                    piInfoList.forEach(e -> {
                        userIds.add(e.getUserId());
                    });
                    queryWrapper.in(OpDeliveryHistory::getUserId, userIds);
                }
            }

            if (params.getSex() != null) {
                LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
                if (params.getSex() != 0) {
                    infoQuery.ge(PiInfo::getSex, params.getSex());
                    List<PiInfo> piInfoList = piInfoService.list(infoQuery);
                    if (CollectionUtils.isEmpty(piInfoList)) {
                        return Result.ok(new Page<>());
                    } else {
                        List<String> userIds = new ArrayList<>();
                        piInfoList.forEach(e -> {
                            userIds.add(e.getUserId());
                        });
                        queryWrapper.in(OpDeliveryHistory::getUserId, userIds);
                    }
                }
            }


            if (params.getMinWorkYear() != null && params.getMaxWorkYear() != null) {
                LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
                int nine1 = 999;
                if (params.getMinWorkYear() == 0 && params.getMaxWorkYear() == nine1) {

                } else if (params.getMinWorkYear() == 0 && params.getMaxWorkYear() == 0) {
                    infoQuery.isNull(PiInfo::getJobTime);
                } else {
                    String start = DateUtil.getYearFirst(params.getMaxWorkYear());
                    String end = DateUtil.getYearLast(params.getMinWorkYear());
                    infoQuery.ge(PiInfo::getJobTime, start).le(PiInfo::getJobTime, end);
                }
                List<PiInfo> piInfoList = piInfoService.list(infoQuery);
                if (CollectionUtils.isEmpty(piInfoList)) {
                    return Result.ok(new Page<>());
                } else {
                    List<String> userIds = new ArrayList<>();
                    piInfoList.forEach(e -> {
                        userIds.add(e.getUserId());
                    });
                    queryWrapper.in(OpDeliveryHistory::getUserId, userIds);
                }
            }

            if (params.getTimeState() != null) {
                if (0 == params.getTimeState()) {
                    queryWrapper.ge(OpDeliveryHistory::getCreateTime, getLastMonths(3));
                } else if (1 == params.getTimeState()) {
                    queryWrapper.ge(OpDeliveryHistory::getCreateTime, getLastMonths(6));
                }
            }
            if(params.getApplyBeginTime() != null && params.getApplyEndTime() != null) {
                queryWrapper.ge(OpDeliveryHistory::getCreateTime,params.getApplyBeginTime());
                queryWrapper.le(OpDeliveryHistory::getCreateTime,params.getApplyEndTime());
            }
            queryWrapper.eq(OpDeliveryHistory::getDelFlag, false).orderByDesc(OpDeliveryHistory::getCreateTime);
            IPage<OpDeliveryHistory> page = deliveryHistoryService.page(params.page(), queryWrapper);
            Function<OpDeliveryHistory, SsUserInfoVO> function = opDeliveryHistory -> {
                SsUserInfoVO vo = new SsUserInfoVO();
                vo.setDataId(opDeliveryHistory.getId());

                LambdaQueryWrapper<CvUserEduInfo> eduQuery = new LambdaQueryWrapper<>();
                eduQuery.eq(CvUserEduInfo::getDelFlag, false).eq(CvUserEduInfo::getUserId, opDeliveryHistory.getUserId()).orderByDesc(CvUserEduInfo::getEduLevel);
                List<CvUserEduInfo> eduInfos = eduInfoService.list(eduQuery);
                vo.setEduInfo(eduInfoConvert.toVO(eduInfos));
                OpJob job = jobService.getById(opDeliveryHistory.getJobId());
                vo.setJobName(job.getJobName());
                vo.setJobTime(opDeliveryHistory.getCreateTime());
                vo.setJobId(job.getId());
                vo.setComId(job.getCompanyId());
                vo.setBsType(job.getBsType());
                vo.setSysFrom(job.getSysFrom());
                vo.setRecruitType(job.getRecruitType());

                EnInfo enInfo = enInfoService.getById(job.getCompanyId());
                vo.setEnInfo(enInfoAdminConvert.toVO(enInfo));
                vo.setDeliveryHistory(deliveryHistoryConvert.toVO(opDeliveryHistory));

                //入职记录
                OpEntryHistory entryHistory = entryHistoryService.lambdaQuery().eq(OpEntryHistory::getDelFlag, false).eq(OpEntryHistory::getUserId, opDeliveryHistory.getUserId())
                        .eq(OpEntryHistory::getJobId, opDeliveryHistory.getJobId()).one();
                if (entryHistory != null) {
                    vo.setEntryHistory(entryHistoryConvert.toVO(entryHistory));
                    EnContacts enContacts = enContactsService.getById(entryHistory.getContactId());
                    vo.getEntryHistory().setContact(contactsConvert.toVO(enContacts));
                }

                //面试信息
                List<OpInterviewHistory> list = interviewHistoryService.lambdaQuery()
                        .eq(OpInterviewHistory::getDelFlag, false).eq(OpInterviewHistory::getJobId, opDeliveryHistory.getJobId())
                        .eq(OpInterviewHistory::getUserId, opDeliveryHistory.getUserId()).orderByDesc(OpInterviewHistory::getCreateTime).list();
                vo.setOpInterviewHistory(interviewHistoryConvert.toVO(list));


                if (!CollectionUtils.isEmpty(vo.getOpInterviewHistory())) {
                    vo.getOpInterviewHistory().forEach(i -> {
                        if (i.getTime() != null) {
                            //面试时间是已过 true 是 false 否
                            i.setTimeFlag(System.currentTimeMillis() > i.getTime().getTime());
                        }

                        EnContacts enContacts1 = enContactsService.getById(i.getContactId());
                        i.setContact(contactsConvert.toVO(enContacts1));

                        List<OpInterviewContact> contact = contactService.lambdaQuery().eq(OpInterviewContact::getDelFlag, false).eq(OpInterviewContact::getViewId, i.getId())
                                .list();
                        if (!CollectionUtils.isEmpty(contact)) {
                            List<PiInfo> contacts = contact.stream().map(el -> piInfoService.lambdaQuery().eq(PiInfo::getUserId, el.getContactId()).one()).filter(el -> el != null).collect(Collectors.toList());
                            i.setConInfo(enPiInfoConvert.toVO(contacts));
                        }

                    });
                }

                //个人信息
                PiInfo piInfo = piInfoService.lambdaQuery().eq(PiInfo::getUserId, opDeliveryHistory.getUserId()).one();
                vo.setUserInfo(enPiInfoConvert.toVO(piInfo));

                if (piInfo != null) {
                    vo.getUserInfo().setWorkYears(piInfo.getWorkYears());
                }

                List<CvUserWorkInfo> workInfos = workInfoService.lambdaQuery().eq(CvUserWorkInfo::getDelFlag, false).eq(CvUserWorkInfo::getUserId, opDeliveryHistory.getUserId()).orderByDesc(CvUserWorkInfo::getCreateTime).list();
                vo.setWorkInfo(workInfoConvert.toVO(workInfos));

                vo.setReId(opDeliveryHistory.getResumeId());
                List<CvWorkIntention> workIntentions = workIntentionService.lambdaQuery().eq(CvWorkIntention::getDelFlag, false).eq(CvWorkIntention::getUserId, opDeliveryHistory.getUserId()).list();
                vo.setWorkIntention(workIntentionConvert.toVO(workIntentions));
                return vo;
            };
            return Result.ok((Page) page.convert(function));
        }
        return Result.ok(new Page<>());
    }

    /**
     * 简历库-人才库-应聘记录
     *
     * @param params
     * @return
     */
    @GetMapping("/applyList")
    @AutoLog(logType = 2, ty = "visit", value = "人才库应聘记录列表")
    public Result<Page<ApplicationHistoryVO>> applyHis(ApplicationHistoryParams params) {
        LambdaQueryWrapper<OpDeliveryHistory> deliveryQuery = new LambdaQueryWrapper<>();
        deliveryQuery.eq(OpDeliveryHistory::getDelFlag, false).eq(OpDeliveryHistory::getUserId, params.getUserId()).orderByDesc(OpDeliveryHistory::getCreateTime);
        IPage<OpDeliveryHistory> page = deliveryHistoryService.page(params.page(), deliveryQuery);

        Function<OpDeliveryHistory, ApplicationHistoryVO> function = deliveryHistory -> {
            ApplicationHistoryVO res = new ApplicationHistoryVO();
            res.setDeliveryInfo(deliveryHistoryConvert.toVO(deliveryHistory));
            OpJob job = jobService.getById(deliveryHistory.getJobId());
            res.setJobInfo(jobConvert.toVO(job));

            EnInfo enInfo = enInfoService.getById(job.getCompanyId());
            res.setEnInfo(enInfoAdminConvert.toVO(enInfo));
            return res;
        };
        return Result.ok((Page) page.convert(function));
    }

    /**
     * 管理端-删除用户投递记录
     * @param params
     * @return
     */
    @PostMapping("/dropInfoById")
    public Result dropInfoById(@RequestBody OpDeliveryHistoryParams params) {
        if (params == null || params.getId() == null) {
            return Result.error("参数错误!");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        OpDeliveryHistory history = deliveryHistoryService.getById(params.getId());
        if (history.getDelFlag()) {
            return Result.error("该记录已被删除，请勿重复操作！");
        } else {
            history.setDelFlag(true).setUpdateBy(sysUser.getId()).setUpdateTime(new Date()).setState(7);
            deliveryHistoryService.updateById(history);
            return Result.ok();
        }
    }
}
